// wxsmall_001/pages/message/index.js
var R_htmlToWxml = require('../../resource/js/htmlToWxml.js');//引入公共方法
var imageUtil = require('../../resource/js/images.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '申请入驻',
    })
    var that = this;
    that.checkuser({
      doServices: function () {

        var userinfo = wx.getStorageSync('userInfo');
        console.log(userinfo.wxInfo);
        that.setData({ userinfo: userinfo })

      }

    }); 



    app.util.request({
      'url': 'entry/wxapp/getbanner',
      success: function (res) {
        if (!res.data.message.errno) {
          that.setData({
            banners: res.data.data,
          })
        }
      },
      complete: function () {

        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh();
      }
    });

    var companyinfo;
    console.log(wx.getStorageSync('companyinfo'));
    companyinfo = R_htmlToWxml.html2json(wx.getStorageSync('companyinfo').description);
    this.setData({
      contact: wx.getStorageSync('companyinfo'),
      companyinfo: companyinfo
    })


  },
  goMap: function (e) {
    wx.openLocation({
      latitude: parseFloat(wx.getStorageSync('companyinfo').lat),
      longitude: parseFloat(wx.getStorageSync('companyinfo').lng),
      scale: 18,
      name: wx.getStorageSync('companyinfo').name,
      address: wx.getStorageSync('companyinfo').address
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindSave: function (e) {
    console.log(e.detail.formId);
    var that = this;
    /*
    app.util.request({
      'url': 'entry/wxapp/savemessage',
      data: { form_id: e.detail.formId, },
      success: function (res) {





      }
    });
    */

    
    var name = e.detail.value.name;
    var tel = e.detail.value.tel;
    var companyname = e.detail.value.companyname;

    if (name == "") {
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel: false
      })
      return
    }
    if (tel == "") {
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel: false
      })
      return
    }

    if (companyname == "") {
      wx.showModal({
        title: '提示',
        content: '请填写备注',
        showCancel: false
      })
      return
    }
    var userinfo = wx.getStorageSync('userInfo');

    app.util.request({
      'url': 'entry/wxapp/savemessage',
      data: { sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid,name: name,tel:tel,companyname:companyname },
      success: function (res) {
       
        
        if (res.data.errno != 0) {
          // 登录错误 
          wx.hideLoading();
          wx.showModal({
            title: '失败',
            content: res.data.data.msg,
            showCancel: false
          })
          return;
        }else{

          wx.showToast({
            title: res.data.data.msg,
            icon: 'success',
            duration: 2000
          })
          /*
          wx.switchTab({
            url: '/weixinmao_house/pages/index/index',
          })
          */
        } 

    

      }
    });

    

  
  }, checkuser: function (options) {


    var that = this;
    var options = options;
    var userinfo = wx.getStorageSync('userInfo');

    if (!userinfo) {
      console.log('tmddddssssss222222');
      app.util.getUserInfo(
        function (userinfo) {

          that.setData({ userinfo: userinfo })
        }
      );
      return false;
    } else {
      if (!userinfo.memberInfo.uid) {
        console.log('tmddddsssssqqqqs1111');
        app.util.getUserInfo(
          function (userinfo) {
            that.setData({ userinfo: userinfo })
          });

        return false;
      } else {

        app.util.request({
          'url': 'entry/wxapp/checkuserinfo',
          data: { sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid },
          success: function (res) {
            if (res.data.data.error == 0) {
              console.log(options);
              options.doServices();

            } else if (res.data.data.error == 2) {
              options.doServices();
              //  options.doElseServices();

            } else {




            }

          }
        });

      }
    }

  },
  onShareAppMessage() {
    return {
      title: '申请入驻'+wx.getStorageSync('companyname').name,
      path: '/weixinmao_house/pages/message/index'
    }
  }
})